import { CurrentUser, Public, RealIp } from '@/decorators';
import { WalletRechangeDto, WalletWithdrawDto } from '@/dtos';
import { WalletService } from '@/services/wallet.service';
import { halt, ok } from '@/utils';
import { InjectQueue } from '@nestjs/bull';
import { Body, Controller, Param, Post, Query } from '@nestjs/common';
import { SkipThrottle, Throttle, seconds } from '@nestjs/throttler';
import { Queue } from 'bull';
import { BaseController } from './base.controller';

@Controller('wallet')
export class WalletController extends BaseController {
  constructor(
    @InjectQueue('bill')
    private readonly billQueue: Queue,
    private readonly walletService: WalletService,
  ) {
    super();
  }

  @Public()
  @Post('sand/callback')
  @SkipThrottle()
  async sandRechargeCallback(
    @Param() params: any,
    @Body() dto: any,
    @Query() query: any,
  ) {
    console.log('杉徳充值回调', params, dto, query);
    // await this.wallerLogQueue.add('cancel', {
    //   no: dto.merOrderId,
    // });
    return 'respCode=000000';
  }

  @Public()
  @Post('uop/callback')
  @SkipThrottle()
  async uopRechargeCallback(@Body() dto: any) {
    console.log('银商充值回调', dto);
    await this.billQueue.add('cancel', {
      no: dto.merOrderId,
    });
    return 'SUCCESS';
  }

  @Post('recharge')
  @SkipThrottle()
  // @Throttle({
  //   short: {
  //     ttl: seconds(3),
  //     limit: 1,
  //   },
  // })
  async recharge(
    @Body() dto: WalletRechangeDto,
    @CurrentUser('id') userId: number,
    @RealIp() ip: string,
  ) {
    return ok(
      await this.walletService.recharge(
        {
          ...dto,
          ip,
        },
        userId,
      ),
    );
  }

  @Post('withdraw')
  @SkipThrottle()
  // @Throttle({
  //   short: {
  //     ttl: seconds(3),
  //     limit: 1,
  //   },
  // })
  async withdraw(
    @Body() dto: WalletWithdrawDto,
    @CurrentUser('id') userId: number,
  ) {
    return ok(await this.walletService.withdraw(dto, userId));
  }
}
